import { _decorator, Component, Node, resources, Sprite, SpriteFrame, Animation, EventTarget, Label } from 'cc';
import { GameData } from './GameData';
import { gameEvent, GameEventType, GameTimer } from './Global';
const { ccclass, property } = _decorator;

@ccclass('Code')
export class Code extends Component {

    // none:默认可点击, success:成功点击, error:错误点击, disable:不可点击，没有颜色
    status: 'none' | 'success' | 'error' | 'disable' = 'none';
    start() {

        this.node.on(Node.EventType.TOUCH_END, this.onClick, this);
    }

    update(deltaTime: number) {

    }

    onClick() {

        if (this.status === 'success') return;
        if (this.status === 'disable') return;
        if (GameTimer.state !== 'playing') return;

        const label = this.node.getChildByName('Label').getComponent(Label);
        const code = label.string;
        if (code === GameData.getNextCode()) {
            const animation = this.node.getComponent(Animation);
            animation.play('code_click_success_anim');
            this.status = 'success';
        } else {
            const animation = this.node.getComponent(Animation);
            animation.play('code_click_error_anim');
            this.status = 'error';
        }
        // 把点击的那个代码块的名字传给事件监听器
        gameEvent.emit(GameEventType.CODE_CLICK, label.string);
    }
}


